/** 单线联系人分析器 */
class SingleContactAnalyzer extends BaseAnalyzer {
    constructor(caseId, targetSubject = null) {
        super(caseId, targetSubject);
        this.name = '单线联系人';
    }
    async analyze(params = {}) {
        const minCalls = params.minCalls || 5;
        const phoneFilter = this.getPhoneFilter();
        const sql = `SELECT opp_user_number, COUNT(*) as total_calls, SUM(duration) as total_duration FROM call_records WHERE case_id = ? ${phoneFilter} GROUP BY opp_user_number HAVING total_calls >= ? ORDER BY total_calls DESC LIMIT 50`;
        const result = await this.runQuery(sql, [this.caseId, minCalls]);
        return this.createResult('single_contact', this.name, result.data || [], `发现 ${result.data?.length || 0} 个高频联系人`, 'info');
    }
    renderResult(data) {
        if (!data || data.length === 0) return '<div class="empty-result" style="padding:40px; text-align:center; color:#999;">✓ 无异常</div>';
        return `<table style="width:100%; border-collapse:collapse;"><thead><tr><th style="padding:10px; border:1px solid #e0e0e0;">对方号码</th><th style="padding:10px; border:1px solid #e0e0e0;">通话次数</th></tr></thead><tbody>${data.map(r => `<tr><td style="padding:8px; border:1px solid #e0e0e0;">${r.opp_user_number}</td><td style="padding:8px; border:1px solid #e0e0e0;">${r.total_calls}</td></tr>`).join('')}</tbody></table>`;
    }
}
window.SingleContactAnalyzer = SingleContactAnalyzer;

/** 断卡行动分析器 */
class SimSwapAnalyzer extends BaseAnalyzer {
    constructor(caseId, targetSubject = null) {
        super(caseId, targetSubject);
        this.name = '断卡行动';
    }
    async analyze(params = {}) {
        const maxDays = params.maxDays || 7;
        const phoneFilter = this.getPhoneFilter();
        const sql = `SELECT opp_user_number, MIN(start_time) as first_call, MAX(start_time) as last_call, CAST((julianday(MAX(start_time)) - julianday(MIN(start_time))) AS REAL) as days_span, COUNT(*) as call_count FROM call_records WHERE case_id = ? ${phoneFilter} GROUP BY opp_user_number HAVING days_span <= ? AND call_count >= 3 ORDER BY call_count DESC LIMIT 50`;
        const result = await this.runQuery(sql, [this.caseId, maxDays]);
        return this.createResult('sim_swap', this.name, result.data || [], `发现 ${result.data?.length || 0} 个短期使用号码`, 'info');
    }
    renderResult(data) {
        if (!data || data.length === 0) return '<div class="empty-result" style="padding:40px; text-align:center; color:#999;">✓ 无异常</div>';
        return `<table style="width:100%; border-collapse:collapse;"><thead><tr><th style="padding:10px; border:1px solid #e0e0e0;">号码</th><th style="padding:10px; border:1px solid #e0e0e0;">使用时长(天)</th></tr></thead><tbody>${data.map(r => `<tr><td style="padding:8px; border:1px solid #e0e0e0;">${r.opp_user_number}</td><td style="padding:8px; border:1px solid #e0e0e0;">${r.days_span.toFixed(1)}</td></tr>`).join('')}</tbody></table>`;
    }
}
window.SimSwapAnalyzer = SimSwapAnalyzer;

/** 事件关联分析器 */
class EventCorrelationAnalyzer extends BaseAnalyzer {
    constructor(caseId, targetSubject = null) {
        super(caseId, targetSubject);
        this.name = '关键事件伴随';
    }
    async analyze(params = {}) {
        const phoneFilter = this.getPhoneFilter();
        const sql = `SELECT user_number, opp_user_number, start_time, duration FROM call_records WHERE case_id = ? ${phoneFilter} ORDER BY start_time DESC LIMIT 50`;
        const result = await this.runQuery(sql, [this.caseId]);
        return this.createResult('event_corr', this.name, result.data || [], `记录 ${result.data?.length || 0} 条通话`, 'info');
    }
    renderResult(data) {
        if (!data || data.length === 0) return '<div class="empty-result" style="padding:40px; text-align:center; color:#999;">✓ 无异常</div>';
        return `<table style="width:100%; border-collapse:collapse;"><thead><tr><th style="padding:10px; border:1px solid #e0e0e0;">时间</th><th style="padding:10px; border:1px solid #e0e0e0;">对方号码</th></tr></thead><tbody>${data.slice(0, 20).map(r => `<tr><td style="padding:8px; border:1px solid #e0e0e0;">${this.formatDate(r.start_time)}</td><td style="padding:8px; border:1px solid #e0e0e0;">${r.opp_user_number}</td></tr>`).join('')}</tbody></table>`;
    }
}
window.EventCorrelationAnalyzer = EventCorrelationAnalyzer;
